package zhumanbing;

import java.util.Random;
import java.util.Scanner;

public class DoubleBall {
    public static void main(String[] args) {
        // 生成中奖号码
        int[] winningNumbers = generateWinningNumbers();
        // 获取用户输入的号码
        int[] userNumbers = getUserInput();
        // 统计红球和蓝球的匹配数量
        int redMatch = countRed(winningNumbers, userNumbers);
        int blueMatch = countBlue(winningNumbers, userNumbers);
        // 确定中奖等级
        String prize = determinePrize(redMatch, blueMatch);
        // 输出结果
        printResult(winningNumbers, userNumbers, redMatch, blueMatch, prize);
    }

    // 生成中奖号码
    public static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        Random random = new Random();
        boolean[] usedRed = new boolean[34]; // 用于标记 1 - 33 的红球是否已被使用
        int count = 0;
        while (count < 6) {
            int red = random.nextInt(33) + 1;
            if (!usedRed[red]) {
                numbers[count] = red;
                usedRed[red] = true;
                count++;
            }
        }
        numbers[6] = random.nextInt(16) + 1; // 生成蓝球号码
        return numbers;
    }

    // 获取用户输入的号码
    public static int[] getUserInput() {
        int[] numbers = new int[7];
        Scanner scanner = new Scanner(System.in);
        boolean[] usedRed = new boolean[34]; // 用于标记用户输入的 1 - 33 的红球是否已存在
        System.out.println("请输入 6 个不重复的 1 - 33 之间的红球号码：");
        for (int i = 0; i < 6; i++) {
            while (true) {
                int num = scanner.nextInt();
                if (num >= 1 && num <= 33 && !usedRed[num]) {
                    numbers[i] = num;
                    usedRed[num] = true;
                    break;
                } else {
                    System.out.println("输入无效，请重新输入 1 - 33 之间且不重复的红球号码：");
                }
            }
        }
        System.out.println("请输入 1 个 1 - 16 之间的蓝球号码：");
        while (true) {
            int num = scanner.nextInt();
            if (num >= 1 && num <= 16) {
                numbers[6] = num;
                break;
            } else {
                System.out.println("输入无效，请重新输入 1 - 16 之间的蓝球号码：");
            }
        }
        return numbers;
    }

    // 统计红球的匹配数量
    public static int countRed(int[] winning, int[] user) {
        int count = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winning[i] == user[j]) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    // 统计蓝球的匹配数量
    public static int countBlue(int[] winning, int[] user) {
        return winning[6] == user[6] ? 1 : 0;
    }

    // 确定中奖等级
    public static String determinePrize(int red, int blue) {
        if (red == 6 && blue == 1) {
            return "一等奖";
        } else if (red == 6 && blue == 0) {
            return "二等奖";
        } else if (red == 5 && blue == 1) {
            return "三等奖";
        } else if ((red == 5 && blue == 0) || (red == 4 && blue == 1)) {
            return "四等奖";
        } else if ((red == 4 && blue == 0) || (red == 3 && blue == 1)) {
            return "五等奖";
        } else if (blue == 1) {
            return "六等奖";
        } else {
            return "未中奖";
        }
    }

    // 输出结果
    public static void printResult(int[] winning, int[] user, int redMatch, int blueMatch, String prize) {
        System.out.print("中奖号码是：");
        for (int i = 0; i < 7; i++) {
            if (i == 6) {
                System.out.print(" | " + winning[i]);
            } else {
                System.out.print(winning[i] + " ");
            }
        }
        System.out.println();
        System.out.print("您输入的号码是：");
        for (int i = 0; i < 7; i++) {
            if (i == 6) {
                System.out.print(" | " + user[i]);
            } else {
                System.out.print(user[i] + " ");
            }
        }
        System.out.println();
        System.out.println("您命中了 " + redMatch + " 个红球和 " + blueMatch + " 个蓝球。");
        System.out.println(prize.equals("未中奖") ? "很遗憾，您未中奖。" : "恭喜您，中了 " + prize + "！");
    }
}